package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.entity.*;
import com.itheima.mapper.OrdersMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
//继承service实现类,帮忙重写BaseMapper<>里面的方法
public class OrderServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    AddressBookService addressBookService;
    @Autowired
    UserService userService;

    @Override
    public void sumbit(Orders orders) {
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook=addressBookService.getById(addressBookId);
        Long userId= BaseContext.getCurrentId();
        long id = IdWorker.getId();
        BigDecimal totalPrice = new BigDecimal(0);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(ShoppingCart::getUserId, userId);

        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);

        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart cart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setName(cart.getName());
            orderDetail.setOrderId(id);
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setAmount(cart.getAmount());
            totalPrice= totalPrice.multiply(new BigDecimal(cart.getNumber()));
            orderDetail.setImage(cart.getImage());
            orderDetailList.add(orderDetail);

        }

        orders.setId(id);
        orders.setNumber(String.valueOf(id));
        orders.setStatus(2);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(totalPrice);
        User user = userService.getById(userId);
        orders.setUserName(user.getName());
        orders.setPhone(user.getPhone());
        orders.setConsignee(addressBook.getConsignee());
        orders.setAddress(addressBook.getDetail());

        this.save(orders);

        orderDetailService.saveBatch(orderDetailList);

        shoppingCartService.remove(queryWrapper);
    }
}
